<template>
  <!-- 新增员工弹窗 -->
  <el-dialog
    title="新增员工"
    :visible="visible"
    :close-on-click-modal="false"
    width="40%"
    @close="close"
    @open="open"
  >
    <!-- 弹框中的表单 -->
    <el-form ref="formRef" :model="formData" :rules="formRules" label-width="80px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="formData.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" type="date" placeholder="请选择入职时间" style="width: 100%" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" placeholder="请选聘用形式" style="width: 100%">
          <el-option v-for="item in hireList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-cascader
          v-model="formData.departmentName"
          style="width:100%"
          :options="departList"
          :props="departProps"
          :show-all-levels="false"
          placeholder="请选择部门"
          @change="handleChange"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" type="date" placeholder="请选择转正时间" style="width: 100%;" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getDepartment, addEmployee } from '@/api'
import transTree from '@/utils/transTree'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 对话框里的表单数据
      formData: {
        username: '', // 姓名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      // 表单校验规则
      formRules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '姓名长度为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'blur' }
        ]
      },
      // 聘用形式数据
      hireList: [
        { id: 1, name: '正式' },
        { id: 2, name: '非正式' }
      ],
      // 部门列表数据
      departList: [],
      // 部门级联选择器
      departProps: {
        value: 'name',
        label: 'name',
        checkStrictly: true,
        expandTrigger: 'hover'
      }
    }
  },
  methods: {
    // 弹窗关闭触发
    close() {
      this.$emit('close')
      // 对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      this.$refs.formRef.resetFields()
      // 手动清空部门的值
      this.formData.departmentName = ''
    },
    // 弹窗打开触发
    async open() {
      const { data } = await getDepartment()
      // console.log(data)
      this.departList = transTree(data.depts)
      // console.log(this.departList)
    },
    // 级联选择器改变时触发
    handleChange(val) {
      console.log(val)
      // 两中方法都可以取到数组中的最后一项元素
      // this.formData.departmentName = val[val.length - 1]
      this.formData.departmentName = val.pop()
    },
    // 提交事件
    submit() {
      // 兜底校验
      this.$refs.formRef.validate(async(valid) => {
        if (!valid) return
        await addEmployee(this.formData)
        // 调用关闭事件
        this.close()
        // 刷新列表
        this.$emit('fetch-employee-list')
        // 提示用户
        this.$message.success('新增员工成功')
      })
    }
  }
}
</script>

<style>
.dialog-footer{
  display: block;
  text-align: center;
}
</style>
